package com.yaodian100.ec.activity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.ec.activity.entity.ActivityPageBlock;
import com.yaodian100.ec.activity.entity.ActivityPageBlockTemplate;
import com.yaodian100.ec.activity.service.ActivityPageBlockService;
import com.yaodian100.ec.activity.service.ActivityPageBlockTemplateService;
import com.yaodian100.ec.common.web.AbstractAction;

public class ActivityPageBlockAction extends AbstractAction {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ActivityPageBlockService activityPageBlockService;
	@Autowired
	private ActivityPageBlockTemplateService activityPageBlockTemplateService;

	private Long pageId;
	private List<ActivityPageBlock> blockList;
	
	private Long blockTemplateId;
	private List<ActivityPageBlockTemplate> blockTemplates;
	
	private ActivityPageBlock block = null;
	
	
	// 活动商品区块排序
	private boolean sortOrderResult = false;
	private String[] blockOrders;
	
	public String listBlock() {
		if (pageId == null) return null;
		
		this.blockTemplates = this.activityPageBlockTemplateService.allTemplateList();
		
		CommonCriteria c = new CommonCriteria();
		c.addEq("pageId", this.pageId);
		String[] sortOrder = new String[] {"sortOrder asc"};
		
		this.blockList = this.activityPageBlockService.getList(0, -1, c, sortOrder);
		return "listBlock";
		
	}
	
	public String addBlock() {
		this.block.setPageId(this.pageId);
		ActivityPageBlockTemplate blockTemplate = this.activityPageBlockTemplateService.get(blockTemplateId);
		this.block.setTemplate(blockTemplate);
		this.activityPageBlockService.saveWithOrder(block);
		return "editPage";
	}
	
	public String delBlock() {
		this.activityPageBlockService.delBlockResetOrder(block.getId());
		return "editPage";
	}
	
	public String saveBlockOrder() {
		try {
			this.activityPageBlockService.updateBlockOrder(blockOrders);
			this.sortOrderResult = true;
		} catch (Exception e) {
			e.printStackTrace();
			this.sortOrderResult = false;
		}
		return "jsonSortOrderResult";
	}
	

	public ActivityPageBlockService getActivityPageBlockService() {
		return activityPageBlockService;
	}

	public void setActivityPageBlockService(
			ActivityPageBlockService activityPageBlockService) {
		this.activityPageBlockService = activityPageBlockService;
	}

	

	public Long getPageId() {
		return pageId;
	}

	public void setPageId(Long pageId) {
		this.pageId = pageId;
	}

	public List<ActivityPageBlock> getBlockList() {
		return blockList;
	}

	public void setBlockList(List<ActivityPageBlock> blockList) {
		this.blockList = blockList;
	}

	public List<ActivityPageBlockTemplate> getBlockTemplates() {
		return blockTemplates;
	}

	public void setBlockTemplates(List<ActivityPageBlockTemplate> blockTemplates) {
		this.blockTemplates = blockTemplates;
	}

	public ActivityPageBlockTemplateService getActivityPageBlockTemplateService() {
		return activityPageBlockTemplateService;
	}

	public void setActivityPageBlockTemplateService(
			ActivityPageBlockTemplateService activityPageBlockTemplateService) {
		this.activityPageBlockTemplateService = activityPageBlockTemplateService;
	}

	public Long getBlockTemplateId() {
		return blockTemplateId;
	}

	public void setBlockTemplateId(Long blockTemplateId) {
		this.blockTemplateId = blockTemplateId;
	}

	public ActivityPageBlock getBlock() {
		return block;
	}

	public void setBlock(ActivityPageBlock block) {
		this.block = block;
	}

	public boolean isSortOrderResult() {
		return sortOrderResult;
	}

	public void setSortOrderResult(boolean sortOrderResult) {
		this.sortOrderResult = sortOrderResult;
	}

	public String[] getBlockOrders() {
		return blockOrders;
	}

	public void setBlockOrders(String[] blockOrders) {
		this.blockOrders = blockOrders;
	}

	
	

}
